package com.luoluo.service.impl;


import com.luoluo.dao.LeaveWordMapper;
import com.luoluo.dao.TouristsLeaveWordMapper;
import com.luoluo.pojo.LeaveWord;
import com.luoluo.pojo.TouristsLeaveWord;
import com.luoluo.pojo.builder.LeaveWordBuilder;
import com.luoluo.pojo.builder.TouristsLeaveWordBuilder;
import com.luoluo.service.LeaveWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
public class LeaveWordServiceImpl implements LeaveWordService {
    @Autowired
    LeaveWordMapper leaveWordMapper;
    @Autowired
    TouristsLeaveWordMapper touristsLeaveWordMapper;

    @Override
    public List<LeaveWord> selectPart(Integer number) {
        return leaveWordMapper.selectPart(number);
    }

    @Override
    @Transactional
    public String touristsLeaveWord(HttpServletRequest request) {
        //可以优化成使用建造者模式
        String leaveWord = request.getParameter("leaveWord");
        String name = request.getParameter("name");

        System.out.println(name+"\t<==>\t"+leaveWord);

        LeaveWord leaveWord1 = new LeaveWordBuilder()
                .builderLeaveWord(leaveWord)
                .builderEffective(1)
                .builderTime(new Date())
                .build();
        int insert = leaveWordMapper.insert(leaveWord1);

        TouristsLeaveWord touristsLeaveWord = new TouristsLeaveWordBuilder()
                .builderName(name)
                .builderIp(request.getLocalAddr())
                .builderEffective(1)
                .builderLeaveWordId(leaveWord1.getId())
                .build();

        insert+=touristsLeaveWordMapper.insert(touristsLeaveWord);
        if(insert>0){
            return "SUCCESS:留言成功";
        }
        return "FAILURE:留言失败";
    }

    @Override
    public LeaveWord selectById(Integer id) {
        return leaveWordMapper.selectByPrimaryKey(id);
    }
}